import axios from 'axios'
import store from '../store'
// import Cookie from 'js-cookie'
// import qs from 'qs';

// 跨域认证信息 header 名
// const xsrfHeaderName = 'Admin-Token'

axios.defaults.timeout = 5000
axios.defaults.withCredentials = true
// axios.defaults.headers['content-type'] = 'application/x-www-form-urlencoded'
// axios.defaults.headers[xsrfHeaderName] = checkAuthorization()
// axios.defaults.xsrfCookieName = xsrfHeaderName

// 认证类型
// const AUTH_TYPE = {
//   BEARER: 'Bearer',
//   BASIC: 'basic',
//   AUTH1: 'auth1',
//   AUTH2: 'auth2',
// }

// http method
const METHOD = {
  GET: 'get',
  POST: 'post'
}

/**
 * axios请求
 * @param url 请求地址
 * @param method {METHOD} http method
 * @param params 请求参数
 * @returns {Promise<AxiosResponse<T>>}
 */
async function request(url, method, params, config) {
  //  console.log()
 
  axios.defaults.headers = store.getters['account/getHeader']


  switch (method) {
    case METHOD.GET:
      return axios.get(url, { params, ...config })
    case METHOD.POST:
      return axios.post(url, params, config)
    default:
      return axios.get(url, { params, ...config })
  }
}

/**
 * 设置认证信息
 * @param auth {Object}
 * @param authType {AUTH_TYPE} 认证类型，默认：{AUTH_TYPE.BEARER}
 */
function setAuthorization(tokenMsg) {
  // switch (authType) {
  //   case AUTH_TYPE.BEARER:
  // alert( tokenMsg.token)
  // sessionStorage.setItem(xsrfHeaderName, tokenMsg.token);
      // Cookie.set('test', '321')
      // adminApi=1mtb6jar38bm2hk3vjf27etj5f;
      // break
    // case AUTH_TYPE.BASIC:
    // case AUTH_TYPE.AUTH1:
    // case AUTH_TYPE.AUTH2:
    // default:
    //   break
  // }
}

/**
 * 移出认证信息
 * @param authType {AUTH_TYPE} 认证类型
 */
// function removeAuthorization(authType = AUTH_TYPE.BEARER) {
//   switch (authType) {
//     case AUTH_TYPE.BEARER:
//       Cookie.remove(xsrfHeaderName)
//       break
//     case AUTH_TYPE.BASIC:
//     case AUTH_TYPE.AUTH1:
//     case AUTH_TYPE.AUTH2:
//     default:
//       break
//   }
// }

/**
 * 检查认证信息
 * @param authType
 * @returns {boolean}
 */
function checkAuthorization() {
  // alert(sessionStorage.getItem(xsrfHeaderName))
  
  // return  sessionStorage.getItem(xsrfHeaderName)
  //  return  Cookie.get(xsrfHeaderName)
}

/**
 * 加载 axios 拦截器
 * @param interceptors
 * @param options
 */
function loadInterceptors(interceptors, options) {
  // const { request, response } = interceptors   //403 404
  // 加载请求拦截器
  // request.forEach(item => {
  //   let { onFulfilled, onRejected } = item
  //   if (!onFulfilled || typeof onFulfilled !== 'function') {
  //     onFulfilled = config => config
  //   }
  //   if (!onRejected || typeof onRejected !== 'function') {
  //     onRejected = error => Promise.reject(error)
  //   }
    // axios.interceptors.request.use(
    //   config => onFulfilled(config, options),
    //   error => onRejected(error, options)
    // )
  // })
  // 加载响应拦截器
  // response.forEach(item => {
    // let { onFulfilled, onRejected } = response     
  //   //onFulfilled 错误时返回 onRejected正确时返回  
  //   if (!onFulfilled || typeof onFulfilled !== 'function') {
  //     onFulfilled = response => response
  //   }
  //   if (!onRejected || typeof onRejected !== 'function') {
  //     onRejected = error => Promise.reject(error)
  //   }
    // axios.interceptors.response.use(
    //   response => onFulfilled(response, options) //返回正确信息
    //   ,
    //   error => onRejected(error, options)  //返回错误信息
    // )
  // })

  const { request, onResolve ,onRejected } = interceptors   //403 404
  let   { onFulfill, onReject } = request

 axios.interceptors.request.use(                 //请求拦截器
      config => onFulfill(config, options),
      error => onReject(error, options)
    )
   
axios.interceptors.response.use(
      response => onResolve(response, options) //返回正确信息
      ,
      error =>onRejected(error, options)  //返回错误信息
    )


}

/**
 * 解析 url 中的参数
 * @param url
 * @returns {Object}
 */
function parseUrlParams(url) {
  const params = {}
  if (!url || url === '' || typeof url !== 'string') {
    return params
  }
  const paramsStr = url.split('?')[1]
  if (!paramsStr) {
    return params
  }
  const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
  for (let i = 0; i < paramsArr.length / 2; i++) {
    const value = paramsArr[i * 2 + 1]
    params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
  }
  return params
}

export {
  METHOD,
  // AUTH_TYPE,
  request,
  setAuthorization,
  // removeAuthorization,
  // checkAuthorization,
  loadInterceptors,
  parseUrlParams
}
